package com.naiterui.ehp.ps.sms.factory;

import com.alibaba.fastjson.JSONObject;
import com.naiterui.ehp.ps.sms.enums.HongdouStatus;
import com.naiterui.ehp.ps.sms.exception.ExceptionCodes;
import com.naiterui.ehp.ps.sms.exception.SmsBusinessException;
import com.naiterui.ehp.ps.sms.vo.MessageResponseVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

/**
 * 红豆电信短信平台
 * 
 * @author yangfuya
 * @since 2022/1/24
 */
@Slf4j
public class HongdouSms {
    /**
     * 发送短信
     * 
     * @param phone
     * @param content
     * @return
     * @throws SmsBusinessException
     * @throws UnsupportedEncodingException
     * @throws NoSuchAlgorithmException
     */
    public static MessageResponseVO siggleSend(String phone, String content) throws SmsBusinessException {
        // 发送短信
        String result = HongdouApi.siggleSend(phone, content);
        log.info("[发送短信，红豆平台]--短信发送完成，手机号电话号码:[{}]  模板参数:[{}] 第三方平台返回结果:[{}]", phone, content, result);
        MessageResponseVO messageResponseVO = processApiResult(result);
        return messageResponseVO;
    }

    /**
     * 处理API返回的结果 CloopenSms.processApiResult()
     *
     * @param result
     *
     */
    private static MessageResponseVO processApiResult(String result) throws SmsBusinessException {
        // 处理api请求结果
        if (StringUtils.isEmpty(result)) {
            log.error("红豆短信平台返回结果为空：[{}]", result);
            throw new SmsBusinessException(ExceptionCodes.SMS_PLATFORM_RETURN_NULL);
        }
        JSONObject resultJson = JSONObject.parseObject(result);
        MessageResponseVO responseVO = new MessageResponseVO();
        String statusCode = resultJson.getString("code");
        if (HongdouStatus.SUCCESS.code.equals(statusCode)) {
            responseVO.setStatusCode(String.valueOf(ExceptionCodes.SUCCESS));
            responseVO.setStatusMsg("成功");
        } else {
            Integer covertCode = convertErrorCode(statusCode);
            if (covertCode != null) {
                log.error("红豆平台返回状态码转换错误,statusCode：{}", statusCode);
                throw new SmsBusinessException(covertCode);
            } else {
                responseVO.setStatusCode(statusCode);
                responseVO.setStatusMsg(resultJson.getString("desc"));
            }
        }

        return responseVO;
    }

    /**
     * 转换容联的返回错误码 CloopenSms.convertErrorCode()
     *
     * @param statusCode
     *
     * @return
     *
     * @Author
     * @Date 2017年8月4日
     * @since 1.0.0
     */
    private static Integer convertErrorCode(String statusCode) {
        // 手机号异常
        if (HongdouStatus.PHONE_INVALID.code.equals(statusCode)
            || HongdouStatus.APP_PHONE_UNFILLED.code.equals(statusCode)) {
            return ExceptionCodes.SMS_PHONE_ERROR;
        } else if (HongdouStatus.PHONE_OVERRUN.code.equals(statusCode)
            || HongdouStatus.MSG_OVERRUN.code.equals(statusCode)
            || HongdouStatus.MSG_SIZE_OVERRUN.code.equals(statusCode)) {
            // 请求超过平台限制
            return ExceptionCodes.SMS_REQUEST_OVER_LIMIT;
        }
        // api配置错误
        else if (HongdouStatus.APP_KEY_NOT_VALIAD.code.equals(statusCode)
            || HongdouStatus.ACCOUNT_CLOSE.code.equals(statusCode)
            || HongdouStatus.ACCOUNT_BUSINESS_NOT_VALIAD.code.equals(statusCode)
            || HongdouStatus.BUSINESS_NOT_EXIST.code.equals(statusCode)
            || HongdouStatus.TIMESTAMP_ERROR.code.equals(statusCode)
            || HongdouStatus.TIMESTAMP_EXPIRED.code.equals(statusCode)
            || HongdouStatus.IP_NOT_WHITELIST.code.equals(statusCode)
            || HongdouStatus.INSUFFICIENT_BALANCE.code.equals(statusCode)
            || HongdouStatus.NO_BUSINESS.code.equals(statusCode) || HongdouStatus.EXTEND_ERROR.code.equals(statusCode)
            || HongdouStatus.CONETENT_NO_SIGN.code.equals(statusCode)
            || HongdouStatus.MSG_INCORRECT_FORMAT.code.equals(statusCode)
            || HongdouStatus.NO_DATA.code.equals(statusCode) || HongdouStatus.UNKNOWN_MISTAKE.code.equals(statusCode)) {
            return ExceptionCodes.SMS_HONGDOU_APP_ERROR;
        }
        // api配置未填写
        else if (HongdouStatus.APP_KEY_UNFILLED.code.equals(statusCode)
            || HongdouStatus.APP_CODE_UNFILLED.code.equals(statusCode)
            || HongdouStatus.APP_SIGN_UNFILLED.code.equals(statusCode)
            || HongdouStatus.APP_TIMESTAMP_UNFILLED.code.equals(statusCode)
            || HongdouStatus.MSG_UNFILLED.code.equals(statusCode)
            || HongdouStatus.MSG_IS_EMPTY.code.equals(statusCode)) {
            return ExceptionCodes.SMS_HONGDOU_APP_UNFILLED;
        }
        return null;
    }

}
